package com.ae.shield.common.jei;

import com.ae.shield.ModLib;
import com.ae.shield.common.recipe.assembly.IAssemblyRecipe;
import com.ae.shield.common.items.ItemList;
import com.mojang.blaze3d.matrix.MatrixStack;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.ingredient.IGuiItemStackGroup;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.category.IRecipeCategory;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.resources.I18n;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.ItemStack;
import net.minecraft.tags.ITag;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.*;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.client.gui.GuiUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class AssemblyCategory implements IRecipeCategory<IAssemblyRecipe> {
    public static final ResourceLocation ID = new ResourceLocation(ModLib.MOD_ID, "assembly_category");
    private final IDrawable back;
    private final IDrawable icon;
    private final Random random = new Random();
    private final IDrawable tankOverlay;
    private int energyBar = 0;
    private int tickBar = 0;

    public AssemblyCategory(IGuiHelper helper) {
        back = helper.createDrawable(new ResourceLocation(ModLib.MOD_ID, "textures/gui/assembly_block.png"), 4, 4, 168, 110);
        icon = helper.createDrawableIngredient(new ItemStack(ItemList.ASSEMBLY_BLOCK.get()));
        tankOverlay = helper.createDrawable(new ResourceLocation(ModLib.MOD_ID, "textures/gui/assembly_block.png"), 176, 1, 16, 58);
    }

    @Override
    public ResourceLocation getUid() {
        return ID;
    }

    @Override
    public Class<? extends IAssemblyRecipe> getRecipeClass() {
        return IAssemblyRecipe.class;
    }

    @Override
    public String getTitle() {
        return new TranslationTextComponent("category." + ModLib.MOD_ID + ".assembly").getString();
    }

    @Override
    public IDrawable getBackground() {
        return back;
    }

    @Override
    public IDrawable getIcon() {
        return icon;
    }

    @Override
    public void setIngredients(IAssemblyRecipe assemblyRecipe, IIngredients iIngredients) {
        iIngredients.setInputIngredients(assemblyRecipe.getIngredients());
        Fluid fluid = assemblyRecipe.getFluid();
        ITag<Fluid> fluidTag = assemblyRecipe.getFluidTag();

        if (fluid != null) {
            iIngredients.setInput(VanillaTypes.FLUID, new FluidStack(fluid, assemblyRecipe.getFluidCount()));
        } else if (fluidTag != null) {
            iIngredients.setInput(VanillaTypes.FLUID, new FluidStack(fluidTag.getRandomElement(random), assemblyRecipe.getFluidCount()));
        }

        iIngredients.setOutput(VanillaTypes.ITEM, assemblyRecipe.getRecipeOutput());
    }

    @Override
    public void setRecipe(IRecipeLayout iRecipeLayout, IAssemblyRecipe assemblyRecipe, IIngredients iIngredients) {
        IGuiItemStackGroup itemStackGroup = iRecipeLayout.getItemStacks();
        itemStackGroup.init(0, true, 61 + 7, 7 + 39);
        itemStackGroup.init(1, true, 43 + 7, 7 + 3);
        itemStackGroup.init(2, true, 61 + 7, 7 + 3);
        itemStackGroup.init(3, true, 79 + 7, 7 + 3);
        itemStackGroup.init(4, true, 97 + 7, 7 + 21);
        itemStackGroup.init(5, true, 97 + 7, 7 + 39);
        itemStackGroup.init(6, true, 97 + 7, 7 + 57);
        itemStackGroup.init(7, true, 79 + 7, 7 + 75);
        itemStackGroup.init(8, true, 61 + 7, 7 + 75);
        itemStackGroup.init(9, true, 43 + 7, 7 + 75);
        itemStackGroup.init(10, true, 25 + 7, 7 + 57);
        itemStackGroup.init(11, true, 25 + 7, 7 + 39);
        itemStackGroup.init(12, true, 25 + 7, 7 + 21);
        itemStackGroup.init(13, false, 133, 40 + 6);
        itemStackGroup.set(iIngredients);
        iRecipeLayout.getFluidStacks().init(14, true, 4, 26, 16, 58, 10000, true, tankOverlay);
        List<List<FluidStack>> inputs = iIngredients.getInputs(VanillaTypes.FLUID);
        if (!inputs.isEmpty()) {
            ITag<Fluid> fluidTag = assemblyRecipe.getFluidTag();
            if (fluidTag != null) {
                for (Fluid fluid : fluidTag.getAllElements()) {
                    iRecipeLayout.getFluidStacks().set(14, new FluidStack(fluid, assemblyRecipe.getFluidCount()));
                }
            } else {
                iRecipeLayout.getFluidStacks().set(14, inputs.get(0));
            }
        }
    }


    @Override
    public void draw(IAssemblyRecipe recipe, MatrixStack matrixStack, double mouseX, double mouseY) {
        IRecipeCategory.super.draw(recipe, matrixStack, mouseX, mouseY);
        drawBar(recipe, matrixStack, mouseX, mouseY);
        drawEnergyTip(recipe, matrixStack, mouseX, mouseY);
    }

    private void drawEnergyTip(IAssemblyRecipe recipe, MatrixStack matrixStack, double mouseX, double mouseY) {
        if (159 < mouseX && mouseX < 165 && 24 < mouseY && mouseY < 85) {
            ArrayList<ITextComponent> tips = new ArrayList<>();
            Screen screen = Minecraft.getInstance().currentScreen;
            tips.add(new TranslationTextComponent("jei.ae.shield.assembly.energy").append(new StringTextComponent(String.valueOf(recipe.getEnergy())).mergeStyle(TextFormatting.YELLOW)).appendString(" RF/tick"));
            tips.add(new TranslationTextComponent("jei.ae.shield.assembly.tick").append(new StringTextComponent(String.valueOf(recipe.getTick())).mergeStyle(TextFormatting.BLUE)).appendString(" tick"));
            screen.func_243308_b(matrixStack, tips, (int) mouseX, (int) mouseY);
        }
    }

    private void drawBar(IAssemblyRecipe recipe, MatrixStack matrixStack, double mouseX, double mouseY) {
        Minecraft.getInstance().getTextureManager().bindTexture(new ResourceLocation(ModLib.MOD_ID, "textures/gui/jei.png"));
        //能量条
        if (energyBar >= 58) {
            energyBar = 0;
        } else {
            energyBar++;
        }

        GuiUtils.drawContinuousTexturedBox(matrixStack, 161, 26 + energyBar, 39, 1 + energyBar, 3, 58 - energyBar, 256, 256, 0, 0);

        //时间条
        if (tickBar >= 32) {
            tickBar = 0;
        } else {
            tickBar++;
        }

        GuiUtils.drawContinuousTexturedBox(matrixStack, 61, 72, 1, 64, tickBar, 2, 256, 256, 0, 0);
    }
}
